package link.anyauto.dtbinding.login;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import org.parceler.Parcels;

import link.anyauto.dtbinding.R;
import link.anyauto.dtbinding.base.ActBase;
import link.anyauto.dtbinding.base.BaseNames;
import link.anyauto.dtbinding.databinding.ActLoginBinding;
import link.anyauto.dtbinding.util.GsonUtil;
import link.anyauto.dtbinding.util.StorageUtil;

/**
 * Created by Administrator on 2016/7/26.
 */
public class ActLogin extends ActBase {
    @Override
    protected void setupBinding() {
        binding = DataBindingUtil.setContentView(this, R.layout.act_login);
    }

    @Override
    protected void setupVM() {
        Bundle state = getSavedInstanceState();
        if(state != null) {
            Parcelable value = state.getParcelable(VM_KEY);
            if(value != null) {
                vm = Parcels.unwrap(value);
            }
        }
        if(vm == null) {
            vm = StorageUtil.get(BaseNames.KEY_LOGIN_VM, LoginVM.class);
        }
        if(vm == null) {
            vm = new LoginVM();
        }
        Log.e("vm", GsonUtil.getGson().toJson(vm));
    }

    @Override
    protected void setupHandlers() {
        handlers = new LoginHandlers();
    }

    @Override
    protected void justAfterSetup() {
        handlers.ctx = this;
        handlers.vm = vm;
        binding.setVm(vm);
        binding.setHandlers(handlers);

        WebSettings ws = binding.web.getSettings();
        ws.setAllowFileAccess(true);
        ws.setAllowFileAccessFromFileURLs(true);
        ws.setAllowUniversalAccessFromFileURLs(true);
        binding.web.setWebChromeClient(new WebChromeClient(){
            public void openFileChooser(ValueCallback<Uri> callback, String type, String capture) {
                // android5.0前会执行这个回调。
                Log.e("called", "open");
                ActLogin.this.callback = callback;
                Intent in = new Intent(Intent.ACTION_GET_CONTENT).addCategory(Intent.CATEGORY_OPENABLE).setType("*/*");
                ActLogin.this.startActivityForResult(Intent.createChooser(in, "选择文件"), REQUEST_CODE);
            }

            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                // 这里写跳去选择文件的代码
                Log.e("called", "show");
                ActLogin.this.callback = callback;
                Intent in = new Intent(Intent.ACTION_GET_CONTENT).addCategory(Intent.CATEGORY_OPENABLE).setType("*/*");
                ActLogin.this.startActivityForResult(Intent.createChooser(in, "选择文件"), REQUEST_CODE);
                return false;
            }
        });
        binding.web.loadData("<!DOCTYPE html><html><body><form method=\"post\" enctype=\"multipart/form-data\"><input type=\"file\"/><br/><input type=\"submit\"/></form></body></html>", "text/html", "UTF-8");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            if (data != null) {
                callback.onReceiveValue(data.getData());
            } else {
                callback.onReceiveValue(null);
            }
        } else {
            callback.onReceiveValue(null);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(VM_KEY, Parcels.wrap(vm));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        StorageUtil.save(BaseNames.KEY_LOGIN_VM, vm);
    }

    ValueCallback<Uri> callback;

    LoginHandlers handlers;
    ActLoginBinding binding;
    LoginVM vm;
    static final String VM_KEY = "login.VM";
    int REQUEST_CODE = 1;
}
